package com.mla.fastdfs.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.mla.fastdfs.commom.Result;
import com.mla.fastdfs.dto.UploadUpInfoVO;
import com.mla.fastdfs.mapper.GroupMapper;
import com.mla.fastdfs.pojo.Group;
import com.mla.fastdfs.pojo.UploadUpInfo;
import com.mla.fastdfs.mapper.UploadUpInfoMapper;
import com.mla.fastdfs.pojo.User;
import com.mla.fastdfs.service.IUploadUpInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mla.fastdfs.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 上传明细 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-03-25
 */
@Service
public class UploadUpInfoServiceImpl extends ServiceImpl<UploadUpInfoMapper, UploadUpInfo> implements IUploadUpInfoService {

    @Autowired
    private UploadUpInfoMapper uploadUpInfoMapper;
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private UserService userService;

    @Override
    public List<UploadUpInfoVO> getList(UploadUpInfo record) {
        LambdaQueryWrapper<UploadUpInfo> queryWrapper = new QueryWrapper<UploadUpInfo>().lambda()
                .isNotNull(UploadUpInfo::getFileUrl)
                .eq(ObjectUtil.isNotNull(record.getGroupId()), UploadUpInfo::getGroupId, record.getGroupId())
                .eq(StringUtil.isNotEmpty(record.getUid()), UploadUpInfo::getUid, record.getUid())
                .like(StringUtil.isNotEmpty(record.getFileName()), UploadUpInfo::getFileName, record.getFileName());
        List<UploadUpInfo> list = uploadUpInfoMapper.selectList(queryWrapper);
        List<UploadUpInfoVO> voList = new ArrayList<>();
        for (UploadUpInfo info : list) {
            UploadUpInfoVO vo = new UploadUpInfoVO();
            BeanUtils.copyProperties(info, vo);
            Group group = groupMapper.selectById(info.getGroupId());
            vo.setGroupName(group.getGroupName());
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public Result pageList(UploadUpInfo record, Integer pageNo, Integer pageSize) {
        LambdaQueryWrapper<UploadUpInfo> queryWrapper = new QueryWrapper<UploadUpInfo>().lambda()
                .isNotNull(UploadUpInfo::getFileUrl)
                .eq(ObjectUtil.isNotNull(record.getGroupId()), UploadUpInfo::getGroupId, record.getGroupId())
                .eq(StringUtil.isNotEmpty(record.getUid()), UploadUpInfo::getUid, record.getUid())
                .like(StringUtil.isNotEmpty(record.getFileName()), UploadUpInfo::getFileName, record.getFileName())
                .like(StringUtil.isNotEmpty(record.getAnotherName()), UploadUpInfo::getAnotherName, record.getAnotherName())
                .orderByDesc(UploadUpInfo::getCreateTime);
        PageHelper.startPage(pageNo, pageSize);
        List<UploadUpInfo> list = uploadUpInfoMapper.selectList(queryWrapper);
        List<UploadUpInfoVO> voList = new ArrayList<>();
        for (UploadUpInfo info : list) {
            UploadUpInfoVO vo = new UploadUpInfoVO();
            BeanUtils.copyProperties(info, vo);
            Group group = groupMapper.selectById(info.getGroupId());
            vo.setGroupName(group.getGroupName());
            User user = userService.getOne(new QueryWrapper<User>().lambda().eq(User::getId, info.getUid()));
            if (user != null)
                vo.setUserName(user.getRealName());

            voList.add(vo);
        }
        PageInfo<UploadUpInfoVO> info = new PageInfo<>(voList);

        return Result.ok(info);
    }

    @Override
    public Result getByIdentifier(String identifier, Integer groupid, String uid) {
        UploadUpInfo info = uploadUpInfoMapper.selectOne(new QueryWrapper<UploadUpInfo>().lambda()
                .eq(UploadUpInfo::getFlag, identifier)
                .eq(UploadUpInfo::getGroupId,groupid)
                .eq(UploadUpInfo::getUid,uid)
                .isNotNull(UploadUpInfo::getFileUrl)
        );
        return Result.ok(info);
    }
}
